package com.mutouren.modules.datapush;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mutouren.common.log.LogManager;
import com.mutouren.common.log.Logger;
import com.mutouren.modules.datapush.archives.Reciever;
import com.mutouren.modules.datapush.base.DataPushLogAlias;
import com.mutouren.modules.datapush.base.PushMessage;

@Service
public class ServerProxy {
	
	private final static Logger ReceiveCVSLogger = LogManager.getLogger(DataPushLogAlias.DataPushReceiveCVS.name());
	
	@Resource
	private Reciever reciever;
	
	public boolean receive(PushMessage message) {
		boolean result = reciever.receive(message);
		writeReceiveLog(message, result);
		return result;
	}
		
	/**
	 * 格式: 
	 * logTime,guid,methodAlias,realUrl,requestBody,
	 * methodType,contentType,charset,createTime,isPermit
	 */
	private void writeReceiveLog(PushMessage message, boolean isPermit) {		
		String logMsg = String.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
				(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")).format(new Date()),
				message.getGuid(),
				message.getMethodAlias(),
				message.getUrl(),
				message.getRequestBody(),
				
				message.getMethodType(),
				message.getContentType(),
				message.getCharset(),
				(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(message.getCreateTime()),
				isPermit);
		
		ReceiveCVSLogger.info(logMsg);
	}	
	
}
